home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / ColorSync 2.5.1 SDK / Sample Code / CSDemo 2.5 / ShellSources / appMenus.c < prev    next >
Encoding:
Text File  |  1998-09-09  |  8.4 KB  |  269 lines  |  [TEXT/CWIE]

  1. // Simple framework for Macintosh sample code
  2. //
  3. // David Hayward and Nick Thompson 
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyrite 1994, Apple Computer,Inc
  8. // 
  9. // Application menu handling code such as dimming menu items and 
  10. // dispatching items.  The dispatcher first calls the the MenuProcPtr
  11. // of the frontmost window to see if it could handle the event.  Any
  12. // items not handled by the front windo are handled here.
  13. //
  14. // 9/13/94    nick    first cut
  15. // 9/13/94    nick    implement commands
  16. // 12/13/94    david    several modifications
  17. // 8/23/95    david    added generic prining to the framework
  18.  
  19.  
  20. #include <Types.h>
  21. #include <StandardFile.h>
  22. #include <Devices.h>
  23.  
  24. #include "appGlobals.h"
  25. #include "appMain.h"
  26. #include "appMenus.h"
  27. #include "appErrors.h"
  28. #include "appPrint.h"
  29. #include "appAEvts.h"
  30.  
  31. #include "win.h"
  32. #include "winTables.h"
  33.  
  34.  
  35. /**\
  36. |**| ==============================================================================
  37. |**| PRIVATE FUNCTION PROTOTYPES
  38. |**| ==============================================================================
  39. \**/
  40. void DoAppNewCommand        ( void ) ;
  41. void DoAppOpenCommand        ( void ) ;
  42. void DoAppCloseCommand        ( winHandle win ) ;
  43. void DoAppQuitCommand        ( void ) ;
  44.  
  45.  
  46. /**\
  47. |**| ==============================================================================
  48. |**| PUBLIC FUNCTIONS
  49. |**| ==============================================================================
  50. \**/
  51.  
  52.  
  53. /*------------------------------------------------------------------------------*\
  54.     dimAllMenus
  55.  *------------------------------------------------------------------------------*
  56.         This routine dims all menus and menu items in the menu bar
  57.         It is called by:
  58.             InitMenuBar() which is called when the app is intited,
  59.             DoActivateEvent() which is called whenever a window is brought to front,
  60.             AdjustMenusForPrinting() which is called before GX print dialogs, and
  61.             CloseProcPtrs which are called whenever windows are closed 
  62. \*------------------------------------------------------------------------------*/
  63. void dimAllMenus ( void )
  64. {
  65.     short            menu, item, totalItems ;
  66.     MenuHandle        mhdl ;
  67.  
  68.     for ( menu=mFirstMenu; menu <= mLastMenu; menu++ )
  69.     {
  70.         mhdl = GetMenuHandle( menu ) ;
  71.         DisableItem( mhdl, kWholeMenu ) ;
  72.  
  73.         if (menu==mApple)
  74.             totalItems = 1 ;
  75.         else
  76.             totalItems = CountMItems(mhdl) ;
  77.  
  78.         for ( item=1; item<=totalItems; item++ )
  79.             DisableItem( mhdl, item ) ;
  80.     }
  81. }
  82.         
  83.         
  84. /*------------------------------------------------------------------------------*\
  85.     DoAppAdjustMenus
  86.  *------------------------------------------------------------------------------*
  87.         This routine enables any menus and menu items in the menu bar
  88.         which are able to be handled.
  89.         It is called by:
  90.             InitMenuBar() which is called when the app is intited,
  91.             DoActivateEvent() which is called whenever a window is brought to front, and
  92.             CloseProcPtrs which are called whenever windows are closed 
  93. \*------------------------------------------------------------------------------*/
  94. void DoAppAdjustMenus ( void ) 
  95. {
  96.     MenuHandle        theMenu ;
  97.     short            i, openableTypes ;
  98.  
  99.     dimAllMenus() ;
  100.  
  101.     // let the windows enable the items they can handle
  102.     (void) CallAllWinUpdateMenusProcs() ;
  103.  
  104.     // Call default UpdateMenusProc
  105.     gDefaulltUpdateMenusProc( nil ) ;
  106.  
  107.     // let the famework enable the items it can handle
  108.         for ( i=openableTypes=0; i<gAllocProcMapCount; i++ )
  109.             if (gAllocProcMap[i].filetype != nil )
  110.                     openableTypes++;
  111.  
  112.         theMenu = GetMenuHandle ( mFile ) ;
  113.         EnableItem ( theMenu, kWholeMenu ) ;
  114.         if (openableTypes) EnableItem ( theMenu, iOpen ) ;
  115.         EnableItem ( theMenu, iQuit ) ;
  116.     
  117.     DrawMenuBar() ;
  118. }
  119.  
  120.  
  121. /*------------------------------------------------------------------------------*\
  122.     HandleMenuCommand
  123.  *------------------------------------------------------------------------------*
  124.         This routine handles all commands generated by mouse event or
  125.         by command key equivalent.  This routine first calls the the MenuProcPtr
  126.         of the frontmost window to see if it could handle the event.  After that
  127.         it hadles any menu items which the application is responsible for.
  128.         It is called by:
  129.             DoMouseDownEvent() which is called in response to mouse events, and
  130.             DoKeyDownEvent() which is called in response to keyboard events
  131. \*------------------------------------------------------------------------------*/
  132. void HandleMenuCommand ( long menuResult )
  133. {
  134.     short            menuID ;
  135.     short            menuItem ;
  136.     Str255            daName ;
  137.     winHandle        win ;
  138.     Boolean            didit = false ;
  139.  
  140.     // see if any of the windows can handle the event
  141.     (void) CallAllWinMenuProcs( menuResult, &didit) ;
  142.     if (didit) return ; // check to see if a window handled it 
  143.  
  144.     // Call default MenuProc
  145.     gDefaulltMenuProc(nil, menuResult, &didit) ;
  146.     if (didit) return ; // check to see if the default handled it 
  147.  
  148.     // if that fails, let the famework do what it can
  149.     win = GetWindowWinHandle( FrontWindow() ) ;
  150.     menuID = HiWrd(menuResult) ;
  151.     menuItem = LoWrd(menuResult) ;
  152.     switch ( menuID )
  153.     {
  154.         case mApple:
  155.             GetMenuItemText(GetMenuHandle(mApple), menuItem, daName) ;
  156.             OpenDeskAcc(daName) ;
  157.             break;
  158.             
  159.         case mFile:
  160.             switch ( menuItem )
  161.             {
  162.                 case iOpen:                // open an existing file
  163.                     DoAppOpenCommand() ;
  164.                     break ;
  165.                     
  166.                 case iClose:            // close an open window/document
  167.                     DoAppCloseCommand( win ) ;
  168.                     break ;
  169.  
  170. #if PIGS_SHELL_PRINT
  171.                 case iPageSetup:        // std page setup dialog
  172.                     DoAppPageSetupCommand( win ) ;
  173.                     break ;
  174.  
  175.                 case iPrint:            // std print job dialog
  176.                     DoAppPrintCommand( win ) ;
  177.                     break ;
  178.  
  179.                 case iPrintOne:            // std print job dialog
  180.                     DoAppPrintOneCommand( win ) ;
  181.                     break ;
  182. #endif
  183.                 case iQuit:
  184.                     DoAppQuitCommand() ;// send ourselves a quit event
  185.                     break ;
  186.             }
  187.             break ;            
  188.     }
  189.     HiliteMenu(0) ;        // Unhighlight whatever MenuSelect or MenuKey hilited
  190. }
  191.  
  192.  
  193. /**\
  194. |**| ==============================================================================
  195. |**| PRIVATE FUNCTIONS
  196. |**| ==============================================================================
  197. \**/
  198.  
  199.  
  200. /*------------------------------------------------------------------------------*\
  201.     DoAppOpenCommand
  202.  *------------------------------------------------------------------------------*
  203.         This routine handles the File:Open command for the application.
  204.         It calls StandardGetFile() and with the resulting FSSpec, calls
  205.         SendODOC() to force the app to send an ODOC event to itself. 
  206.         It is called by:
  207.             HandleMenuCommand() which handles all menu events.
  208. \*------------------------------------------------------------------------------*/
  209. static void DoAppOpenCommand ( void ) 
  210. {
  211.     SFTypeList            *myTypesPtr ;    
  212.     StandardFileReply    theSFReply ;    // Get the file name to open
  213.     short                i, count ;
  214.     OSErr                err ;
  215.  
  216.     myTypesPtr = (SFTypeList*) NewPtr( (Size)( sizeof(long) * gAllocProcMapCount) ) ;
  217.     err = MemError();
  218.     WarnIfErr( err ) ;
  219.     if (err) return ;
  220.  
  221.     for ( i=count=0; i<gAllocProcMapCount; i++ )
  222.         if (gAllocProcMap[i].filetype != nil )
  223.             (*myTypesPtr)[count++] = gAllocProcMap[i].filetype ;
  224.  
  225.     StandardGetFile( nil, count, *myTypesPtr, &theSFReply ) ;
  226.     
  227.     DisposePtr( (Ptr)myTypesPtr ) ;
  228.  
  229.     if(theSFReply.sfGood)                 // if the user did not cancel
  230.         SendODOC( &theSFReply.sfFile ) ;// send ODOC event to ourselves:
  231.  
  232.     return ;
  233. }
  234.  
  235.  
  236.  
  237. /*------------------------------------------------------------------------------*\
  238.     DoAppCloseCommand
  239.  *------------------------------------------------------------------------------*
  240.         This routine handles the File:Close command for the application.
  241.         It calls DisposeWinHandle() and also returns all the menus
  242.         to the default state by calling DoAppAdjustMenus().
  243.         This is needed because, if the window being closed is the only window,
  244.         then there is not another window to trigger an activate event. 
  245.         It is called by:
  246.             HandleMenuCommand() which handles all menu events.
  247. \*------------------------------------------------------------------------------*/
  248. static void DoAppCloseCommand ( winHandle win ) 
  249. {
  250.     CallWinCloseProc( win ) ;
  251.     DoAppAdjustMenus() ;
  252. }
  253.  
  254.  
  255. /*------------------------------------------------------------------------------*\
  256.     DoAppQuitCommand
  257.  *------------------------------------------------------------------------------*
  258.         This routine handles the File:Quit command for the application.
  259.         It calls StandardGetFile() and with the resulting FSSpec, calls
  260.         It calls SendQUIT() to force the app to send an QUIT event to itself. 
  261.         It is called by:
  262.             HandleMenuCommand() which handles all menu events.
  263. \*------------------------------------------------------------------------------*/
  264. static void DoAppQuitCommand ( void ) 
  265. {
  266.     SendQUIT() ;                // send ourselves a quit event
  267.     return ;
  268. }
  269.